/*
 * Copyright (c) 2016 CGT Co., Ltd.
 *
 * Authors: Robin Lee <lixiangbin@china-dsp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program;
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <memory.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <sys/socket.h>

#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#include <signal.h>

#include <termios.h>
#include <netinet/in.h>

#include "appcmd.h"

/*---------------------------------------------------------------------------------------------------------------------------------------------------
 * global vars
 */
#define FP_DEV "/sys/class/i2c-dev/i2c-0/device/0-0002/eeprom"

static int global_fd = -1;

static int fd_initial(void)
{

	int fd = -1;

	/*
		Open modem device for reading and writing and not as controlling tty
		because we don't want to get killed if linenoise sends CTRL-C.
	*/

	printf("Open '%s' .. ", FP_DEV);
	fd = open(FP_DEV, O_RDWR );
	if (fd <0) {
		printf("failed (err = %d)\n", fd);
		return -1;
	}

	printf("Done\n");

	return fd;
}

static int fd_exit(int fd)
{
	printf("Close '%s' .. ", FP_DEV);

	close(fd);

	printf("Done\n");

	return 0;
}

/*---------------------------------------------------------------------------------------------------------------------------------------------------
 * get_1848_reg, set_1848_reg
 */
static unsigned int get_1848_reg(unsigned int offset)
{
	unsigned int value = 0xffff;
	if (global_fd<0) {
		printf("Invalid device handle !\n");
		return value;
	}

	if( lseek(global_fd, offset, SEEK_SET) == (off_t) -1 ) {
		printf("failed for seek to offset 0x%x !\n", offset);
		return value;
	}

	if ( read(global_fd, &value, sizeof(value)) != sizeof(value)) {
		printf("failed for read from offset 0x%x !\n", offset);
		return value;
	}

	return value;
}

static void set_1848_reg(unsigned int offset, unsigned int data)
{
	unsigned int value = data;
	if (global_fd<0) {
		printf("Invalid device handle !\n");
		return ;
	}

	if( lseek(global_fd, offset, SEEK_SET) == (off_t) -1 ) {
		printf("failed for seek to offset 0x%x !\n", offset);
		return ;
	}

	if ( write(global_fd, &value, sizeof(value)) != sizeof(value)) {
		printf("failed for write from offset 0x%x !\n", offset);
		return ;
	}

	return ;
}

static unsigned int regtoul(const char *str)
{
	int cbase = 10;
	if(str[0]=='0'&&(str[1]=='x'||str[1]=='X')) {
		cbase = 16;
	}
	return strtoul(str, NULL, cbase);
}

/*---------------------------------------------------------------------------------------------------------------------------------------------------
 * help
 */
APPCMD_OPTION_FUNCTION(help)
{
	int optn;
	appcmd_options_t *poption, *opt_list = (appcmd_options_t *)p;

	printf("\n** SDI I2C Test Samples, Version: 1.0.1\n");
	printf("\tCopyright (c) 2019 CETC Co., Ltd.\n");

	printf("\n** commands:\n");
	for( optn=0; (poption=&opt_list[optn]) && poption->name; optn++) {
		printf("\t%-10s\t: %s\n", poption->name, poption->help);
	};
	return 0;
}

/*---------------------------------------------------------------------------------------------------------------------------------------------------
 * console
 */

APPCMD_OPTION_FUNCTION(set)
{
	unsigned int offset, value;

	if(argc < 3) {
		printf("Invalid command line for 'set'\n");
		return -1;
	}

	offset = regtoul(argv[1]);
	value = regtoul(argv[2]);

	printf("Set value 0x%x to offset 0x%x .. \n", value, offset);
	
	set_1848_reg(offset, value);

	printf("done \n");
	
	return 0;
}

APPCMD_OPTION_FUNCTION(get)
{
	unsigned int offset, value;

	if(argc < 2) {
		printf("Invalid command line for 'set'\n");
		return -1;
	}

	offset = regtoul(argv[1]);

	printf("Get value from offset 0x%x .. \n", offset);
	
	value = get_1848_reg(offset);

	printf("Return value of offset 0x%x : 0x%x \n", offset, value);

	return 0;
}

#define QUIT_MARK_RETURN	0xdeadbeef

APPCMD_OPTION_FUNCTION(quit)
{
	return QUIT_MARK_RETURN;
}

static appcmd_options_t cmdlist_console[]=
{
	APPCMD_OPTION(set,		"Set register value:\n"
							"\t\t\tset <offset> <value>"
							, NULL),
	APPCMD_OPTION(get,		"Get register value:\n"
							"\t\t\tget <offset>"
							, NULL),
	APPCMD_OPTION(help,		"Help for commands", cmdlist_console),
	APPCMD_OPTION(quit,		"Quit from console", NULL),
	APPCMD_OPTION_NULL,
};

APPCMD_OPTION_FUNCTION(console)
{
	int strl;
	char *str=NULL;
	size_t n=0;
	while(1) {
		printf("\nsdi i2c test> ");
		strl = getline(&str, &n, stdin);
		if(strl <= 0)
			continue;
		if (0==strncmp(str, "quit", 4))
			break;
		appcmd_options_runcmd(cmdlist_console, str);
	};
	if(str)
		free(str);
	return 0;
}

/*---------------------------------------------------------------------------------------------------------------------------------------------------
 * main
 */
static appcmd_options_t cmdlist[]=
{
	APPCMD_OPTION(console,	"Start console for env operation", NULL),
	APPCMD_OPTION(help,		"Show help infomation", cmdlist),
	APPCMD_OPTION_NULL,
};

static char commands[512] = {0,};
int main(int argc, char *argv[])
{
	int argi;

	printf("Linux SDI_I2C_Test APP start(Version 0.1)\r\n");
	if (argc<2) {
		strcpy(commands, "console");
	} else {
		char *str = commands;
		for (argi=1; argi<argc; argi++) {
			int pn = snprintf(str, (commands+sizeof(commands)-4-str), "%s  ", argv[argi]);
			if (pn<=0) break;
			str+= pn;
		}
	}

	global_fd = fd_initial();
	if (global_fd<0) {
		printf("\n Invalid sdi device ! \n");
		return -1;
	}

	appcmd_options_runcmd(cmdlist, commands);

	fd_exit(global_fd);

	return 0;
}
